package rhinestone.addon;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;

/**
 * The class loads the container objects at the start of the program,
 * and saves modified containers before closing the program.
 *
 * @author Group 2
 * @version 2010.12.21
 */
public class ContainerDB {
	public static Object loadContainer(String file) {
		try {
			FileInputStream fileIn = new FileInputStream(file);
			ObjectInputStream in = new ObjectInputStream(fileIn);
			Object container = in.readObject();
			in.close();
			fileIn.close();
			return container;
		} catch(FileNotFoundException ex) {
			System.err.println("Loading container from file " + file + " failed.");
			System.err.println("Creating a new one.");
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public static void saveContainer(Object container, String file) {
		try {
			FileOutputStream fileOut = new FileOutputStream(file);
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(container);
			out.close();
			fileOut.close();
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
}
